package com.yuneec.droneservice.command;

import com.MAVLink.yuneec.msg_mav_rc_cmd_ack;
import com.yuneec.droneservice.model.JoystickMap;
import com.yuneec.droneservice.request.IResponse;

/**
 * Created by gjw on 2017/3/27.
 */

public class GetJoystickMap extends BaseCommand {

    public GetJoystickMap() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_GET_CH_MAP);
    }

    @Override
    protected short[] body() {
        return null;
    }

    @Override
    public IResponse<JoystickMap> toResponse(msg_mav_rc_cmd_ack ack) {
        int code = ack.code;
        IResponse<JoystickMap> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            try {
                if (ack.params == null || ack.params.length < 4) {
                    response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
                    return response;
                }
                JoystickMap joystickMap = new JoystickMap();
                joystickMap.setCh1(ack.params[0] & 0xff);
                joystickMap.setCh2(ack.params[1] & 0xff);
                joystickMap.setCh3(ack.params[2] & 0xff);
                joystickMap.setCh4(ack.params[3] & 0xff);
                response.setParam(joystickMap);
            } catch (Exception e) {
                response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
            }
        }
        return response;
    }

}
